package com.jianglin.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jianglin.vo.R;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author jianglin
 */
@Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        //未授权
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        PrintWriter out = response.getWriter();
        //访问被拒绝时
        R r=R.ACCESS_DENY(authException.getMessage());
        ObjectMapper mapper = new ObjectMapper();
        out.print(mapper.writeValueAsString(r));
        out.flush();
    }
}